package com.credit.management.system.web.controller;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.credit.management.system.model.Contract;
import com.credit.management.system.model.ContractSub;
import com.credit.management.system.model.Page;
import com.credit.management.system.service.IContractService;
import com.credit.management.system.service.IContractSubService;
import com.credit.management.system.service.IProjectService;

@Controller
@RequestMapping("/contract")
public class ContractController {
	@Autowired
	private IContractService contractService;
	@Autowired
	private IProjectService projectService;
	@Autowired
	private IContractSubService contractSubService;
	/**
	 * 进入contract.jsp页面
	 * @return
	 */
	@RequestMapping(value="/contract",method=RequestMethod.GET)
	public String contract(){
		return "contract/contract";
	}
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findlist(Integer start, Integer length)
			throws UnsupportedEncodingException {
		if (start == null) {
			start = 0;
		};
		if (length == null) {
			length = 5;
		};
		// 查总条数（单表查询），先去联系怎删改
		Page page=new Page(start,length);
		Page page1=contractService.findPageContracts(page);	
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aaData", page1.getList());//返回的数据
		map.put("iTotalRecords", page1.getTotal());//总条数
		map.put("iTotalDisplayRecords",page1.getTotal());////总条数
		return map;
	}
	/**
	 * 点击添加按钮，请求后台拿到所有已通过却未签订合同的项目，返回给添加合同弹框
	 * @return
	 */
	@RequestMapping(value="/addContractPre",method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String,Object>> addContractPre(){
		List<Map<String,Object>> maps=projectService.findProjectByState();
		return maps;
	}
/*	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Integer add( String date,Long sum,Long projectId,Long state,Long repaymentMethod){
		//contract.setDate(date);
		SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		Date date1;
		Contract contract=null;
		try {
			date1 = sdf.parse("2008-07-10 19:20:00");
			contract=new Contract (new Date(), sum, projectId, state, repaymentMethod);
			System.out.println("DATE1:"+date1);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Integer result=contractService.insertSelective(contract);
		return result;
	}*/
	
	
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Integer add( Contract contract){
		//contract.setDate(date);
		Integer result=contractService.insertSelective(contract);
		SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		try {
			Date date=sdf.parse(contract.getDate());
			Calendar calendar=sdf.getCalendar();
			calendar.setTime(date);
			//签订日期年份+1
			calendar.add(Calendar.YEAR, +1);
			ContractSub contractSub=new ContractSub();
			contractSub.setContractId(contract.getId());
			//每月应还钱为 （借款*（期数+1））/（期数*100）
			contractSub.setSum((contract.getSum()*(contract.getRepaymentMethod()/3))/contract.getRepaymentMethod());
			//根据借款方式生成相应个数子合同，还款日期为贷款日期的第二年，该月份，该月份+1，该月份+2，以此类推
			for(int i=0;i<contract.getRepaymentMethod();i++){
				calendar.add(Calendar.MONTH, +1);
				Date date1=calendar.getTime();
				String date2=sdf.format(date1);
				contractSub.setRepaymentDate(date2);
				contractSubService.insertSelective(contractSub);
				System.out.println("add_date:"+date2);
			}
			
		
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Integer delete(Long id){
		Integer result=contractService.deleteByPrimaryKey(id);
		return result;
	}
}
